package com.beiding.test.pg;

import com.beiding.sqlbuilder.*;

public class FieldParameterHandler implements ParameterHandler {
    @Override
    public boolean canHandle(Parameter parameter) {
        return parameter instanceof FieldParameter;
    }

    @Override
    public String handle(Parameter parameter) {
        FieldParameter fieldParameter = (FieldParameter) parameter;
        TableField field = fieldParameter.getField();

        Table table = field.getTable();

        String r;

        if (table.getAlias() == null) {
            r = field.getName();
        } else {
            r = table.getAlias() + "." + field.getName();
        }

        if (field.getFun() != null) {
            r = "\"" + fun(field.getFun()) + "\"(" + r + ")";
        }

        if (field.getAs() != null) {
            r = r + " as " + field.getAs();
        }

        return r;
    }


    private String fun(FunType funType) {
        switch (funType) {
            case Count:
                return "count";
        }
        throw new RuntimeException("不支持的函数:" + funType);
    }

}
